#include "GUIListBoxItem.h"
#include "GUIListBox.h"
#include "IRenderer.h"
#include "OnMouseLeftDownEvent.h"

GUIListBoxItem::GUIListBoxItem() : IGUIElement()
{
	m_fontColor = 0xFF000000;
	m_font = HashedString("Arial");
	m_visible = true;
	m_enabled = true;
	m_dimensions = Maths::Point<unsigned int>(1, 1);
}

void GUIListBoxItem::Paint()
{
	if (m_visible)
	{
		// Render background
		Maths::Matrix transform;
		transform.Translate(m_location.X, m_location.Y, 0.0f);
		transform.Scale(m_dimensions.X, m_dimensions.Y, 1.0f);

		Renderer->SetColor(0xFFDDDDDD);
		Renderer->PushTexture(m_background);
		Renderer->PushModelViewMatrix(transform);
		Renderer->Render(*(m_shape.getShape()));
		Renderer->PopModelViewMatrix();
		Renderer->PopTexture();
		Renderer->SetColor(0xFFFFFFFF);

		// Render text
		Renderer->PrintFont(m_font, m_fontColor, m_parentLocation.X + m_location.X+4, m_parentLocation.Y + m_location.Y+4, m_text.c_str());
	}
}

bool GUIListBoxItem::ReceiveEvent( IEvent& event )
{
	if (m_enabled && m_visible)
	{
		if (event.Type() == OnMouseLeftDownEvent::type)
		{
			OnMouseLeftDownEvent& mouseEvent = static_cast<OnMouseLeftDownEvent&>(event);

			int x = mouseEvent.Coords().X;
			int y = mouseEvent.Coords().Y;
			int absX = m_location.X + m_parentLocation.X;
			int absY = m_location.Y + m_parentLocation.Y;
		
			if ((x > absX) && (x < absX + (int)m_dimensions.X) &&
				(y > absY) && (y < absY + (int)m_dimensions.Y))
			{
				((GUIListBox*)m_parent)->Select(this);
				return true;
			}
		}
	}
	return false;
}

void GUIListBoxItem::Text( std::string& text )
{
	m_text = text;
}

std::string GUIListBoxItem::Text() const
{
	return m_text;
}

void GUIListBoxItem::Font( std::string& font )
{
	m_font = HashedString(font);
}

void GUIListBoxItem::Font( HashedString& font )
{
	m_font = font;
}

HashedString GUIListBoxItem::Font() const
{
	return m_font;
}

void GUIListBoxItem::FontColor( unsigned int color )
{
	m_fontColor = color;
}

unsigned int GUIListBoxItem::FontColor() const
{
	return m_fontColor;
}

void GUIListBoxItem::Background( std::string& background )
{
	m_background = HashedString(background);
}

void GUIListBoxItem::Background( HashedString& background )
{
	m_background = background;
}

HashedString GUIListBoxItem::Background() const
{
	return m_background;
}